/**
 * @class   wosh::User
 * @brief   Represents an user of WOSH system
 *
 ****************************************************************************
 * @version $Id: User.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR UserS;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_User_H__
 #define __WOSH_Core_User_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/Variant.h>
 #include <core/MethodsProvider.h>
 #include <core/PropertiesProvider.h>
 #include <core/UserInfoCommmunicators.h>
 #include <core/UserInfoLocation.h>


namespace wosh {

 #define _User_KEY_IsAtHome			"IsAtHome"

 #define _User_EVENT_UserLogged		"UserLogged"


class Request;
class Response;
class UserManager;
class SecurityManager;
template <class T> class DataFieldCollector;
class User;
void user_assign( wosh::User* user, long id );

class User :	public Object,
				public virtual IPropertiesProviderListener,
				public MethodRunner
 {
	WOSH_SERIALIZABLE(wosh::User)
	WOSH_COMPARE_RTTI(wosh::User)

	public:
		enum PRESENCE {
			PRESENCE_UNKNOWN		= 0x00, ///< 00000000 = 0
			PRESENCE_OFFLINE		= 0x41, ///< 00100001 = 65
			PRESENCE_OFFLINE_AWAY	= 0x45, ///< 00100101 = 69
			PRESENCE_ONLINE			= 0x81, ///< 01000001 = 129
			PRESENCE_ONLINE_AWAY	= 0x85, ///< 01000101 = 133
			PRESENCE_ONLINE_DND		= 0x89  ///< 01001001 = 137
		 };

		enum STATUS {
			STATUS_UNKNOWN		= 0x00, ///< 00000000 = 0
			STATUS_LOGGEDIN		= 0x01, ///< 00000001 = 1
			STATUS_LOGGEDOUT	= 0x03  ///< 00000011 = 3
		 };
 

	public:
		/**
		 * @brief  Empty constructor.
		 */
		User();

		User( long id, const std::string& name, const std::string& password );

		User( long id, const std::string& name, const std::string& password, const std::string& groupsAsString  );

		friend class wosh::UserManager;

	public:
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		User( const User& m );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~User();

	public:

		void updateLocation( const std::string& new_location, int64 timestamp, double probability = 0.0 );

		void updateCommmunicator( const std::string& new_communicator, int64 timestamp, double ranking_offset = 0.0 );
		
		void setAtHome( bool value );


/** @name Getters
 * @{
 ******************************************************************************/
	public:

		inline long getID() const							{ return this->id; }

		bool isMemberOf( const std::string& group) const;

		bool isAtHome() const;

		bool isValidPassword( const std::string& password,  const std::string& password_proto = "plain" ) const;

		const std::vector<std::string>& getGroups() const	{ return this->groups; }
		std::vector<std::string>& getGroups()				{ return this->groups; }

		const PropertiesProvider& getProperties() const		{ return this->Properties; }
		PropertiesProvider& getProperties() 				{ return this->Properties; }

		const MethodsProvider& getMethods() const			{ return this->Methods; }
		MethodsProvider& getMethods()						{ return this->Methods; }

		Variant getValueOf( const std::string& key ) const
			{ return Properties.getValueOf(key); }


		UserInfoLocation& getLocation()						{ return this->Location; }
		const UserInfoLocation& getLocation() const 		{ return this->Location; }

		UserInfoCommmunicators& getCommunicators()			{ return this->Communicators; }
		const UserInfoCommmunicators& getCommunicators( )const { return this->Communicators; }

		static DataFieldCollector<User>* getDataFields();

		static const char* getUserPresenceAsString( User::PRESENCE value );
		static const char* getUserStatusAsString( User::STATUS value );

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:

		virtual void setName( const std::string& name );

		virtual void setPermission( const Permission& value );

		virtual void setPermission( const Permission& value, short overwrite_mode, bool when_empty = false );

		WRESULT setProperty( const std::string& key, const Variant& value )
															{ return Properties.set(key, value); }


//@}
/** @name IPropertiesProviderListener interface
 * @{
 ******************************************************************************/
 	public:
		virtual bool readingProperty( Property* , const PropertiesProvider*  ) { return true; }
		virtual bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );

//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual User& operator=(const User& m);
		virtual bool operator==(const User &other) const;
		virtual bool operator!=(const User &other) const	{ return !(*this == other);  }

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const				{ return Utilities::format("%ld:%s", getID(), getName().c_str()); }
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
	protected:

		void initUser();
		virtual void clear();


	protected:
		Response* mmDoConnect( const Request* request );
		Response* mmDoDisconnect( const Request* request );

	protected:
		long id;						///< Unique ID of the user
		std::string pwd;				///< login password 
		std::string pwd_proto;			///< protocol of the password {plain,md5, ..}

		std::vector<std::string> groups;

		mutable MethodsProvider Methods;
		mutable PropertiesProvider Properties;	///< properties collection


		UserInfoLocation Location;
		UserInfoCommmunicators Communicators;

	protected:
		friend class wosh::SecurityManager;
		friend void user_assign( User* user, long id );

	public:

		static std::string root;

}; // class def

}; // namespace wosh

#endif //__WOSH_Core_User_H__
